package com.ruoyi.womp.task;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.womp.service.ISqlQueryService;
import com.ruoyi.womp.service.RealTimeServie;
import com.ruoyi.womp.tools.DateUtil;

@Component("workOrderOsiTask")
public class OsiWorkOrderTask {

	final static String TASK_NAME = "巡检工单生成任务";

	@Autowired
	ISqlQueryService sqlQueryService;

	@Autowired
	RealTimeServie realTimeServie;

	public void execute() {
		handleData();
	}

	private void handleData() {
		try {
			// 查询设置自动生成巡检工单且有责任人的衡器信息，包含最后巡检时间
			String sql = " SELECT wea.type,wea.code,wea.charger,plan_type, "
					+ " (select max(osi_date) from womp_osi_record wor where wor.wa_code=wea.code and wor.osi_plan_type=wop.plan_type) last_osi_date "
					+ " FROM womp_weighing_apparatus wea inner join womp_osi_plan wop on wea.code=wop.wa_code "
					+ " where wea.auto_workorder='Y' and charger is not null ";
			List<Map<String, Object>> waList = sqlQueryService.queryMapList(sql);

			for (Map<String, Object> waMap : waList) {
				// 巡检类型
				String planType = waMap.get("plan_type").toString();
				// 最后巡检日期
				String lastOsiDate = null == waMap.get("last_osi_date") ? null : waMap.get("last_osi_date").toString();
				// 衡器编号
				String waCode = waMap.get("code").toString();
				// 衡器巡检责任人
				String charger = waMap.get("charger").toString();

				// 周检
				if (planType.equals("10")) {
					// 计算最后巡检日期与当前时间相关天数，如达到天数，则下发巡检工单
					if (null == lastOsiDate || checkOsiDate(lastOsiDate, 7)) {
						// 查询是否有相同未处理工单
						if (!hasUnHandleWorkorder(waCode, "10")) {
							realTimeServie.createWorkorder(waCode, "10", charger, "系统生成");
						}
					}
				}
				// 月检
				else if (planType.equals("20")) {
					// 计算最后巡检日期与当前时间相关天数，如达到天数，则下发巡检工单
					if (null == lastOsiDate || checkOsiDate(lastOsiDate, 30)) {
						if (!hasUnHandleWorkorder(waCode, "20")) {
							realTimeServie.createWorkorder(waCode, "20", charger, "系统生成");
						}
					}
				}
				// 半年检
				else if (planType.equals("30")) {
					// 计算最后巡检日期与当前时间相关天数，如达到天数，则下发巡检工单
					if (null == lastOsiDate || checkOsiDate(lastOsiDate, 180)) {
						if (!hasUnHandleWorkorder(waCode, "30")) {
							realTimeServie.createWorkorder(waCode, "30", charger, "系统生成");
						}
					}
				}
				// 年检
				else if (planType.equals("40")) {
					// 计算最后巡检日期与当前时间相关天数，如达到天数，则下发巡检工单
					if (null == lastOsiDate || checkOsiDate(lastOsiDate, 365)) {
						if (!hasUnHandleWorkorder(waCode, "40")) {
							realTimeServie.createWorkorder(waCode, "40", charger, "系统生成");
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private boolean checkOsiDate(String date, int days) throws ParseException {
		Date lastOsiDate = DateUtil.parseDate(date, "yyyy-MM-dd");

		long diff = Calendar.getInstance().getTimeInMillis() - lastOsiDate.getTime();
		long diffDays = diff / (24 * 60 * 60 * 1000);
		if (diffDays >= days) {
			return true;
		}
		return false;
	}

	private boolean hasUnHandleWorkorder(String waCode, String osiType) {
		String sql = " SELECT * FROM womp_work_order where wa_code='" + waCode + "' and osi_type='" + osiType
				+ "' and state=0 ";
		List<Map<String, Object>> list = sqlQueryService.queryMapList(sql);
		if (null == list || list.size() == 0) {
			return false;
		}
		return true;
	}

}